﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using QuanCafe.BusinessLayer;
using QuanCafe.DataLayer;

namespace QuanCafe.admin
{
    public partial class District : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.fnLoadData();
                loadCboProvince();
            }
        }

        private void loadCboProvince()
        {
            QuanCafe.BusinessLayer.Province Province = new QuanCafe.BusinessLayer.Province();
            cboProvinceID.DataSource = Province.GetAll();
            cboProvinceID.DataTextField = "ProvinceName";
            cboProvinceID.DataValueField = "ProvinceID";
            cboProvinceID.DataBind();
        }
        /// <summary>
        /// load data
        /// </summary>
        private void fnLoadData()
        {
            try
            {
                QuanCafe.BusinessLayer.District District = new QuanCafe.BusinessLayer.District();
                DataTable dtb = District.GetAll();
                dtgDistrict.DataSource = dtb;
                dtgDistrict.DataBind();
                for (int i = 0; i < dtgDistrict.Items.Count; i++)
                {
                    if (dtb.Rows[i]["Status"].ToString() == "1")
                    {
                        ((ImageButton)dtgDistrict.Items[i].FindControl("btnActive")).ImageUrl = "~/Images/icons/btnOK.jpg";

                    }
                    else
                    {
                        ((ImageButton)dtgDistrict.Items[i].FindControl("btnActive")).ImageUrl = "~/Images/icons/btnKO.jpg";
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        public void fnClearControl()
        {
            txtDistrictID.Text = "";
            txtDistrictName.Text = "";
            txtDistrictCode.Text = "";
            txtNote.Text = "";
            lblError.Text = "";
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            fnClearControl();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                QuanCafe.BusinessLayer.District DistrictObj = new QuanCafe.BusinessLayer.District();
                if (txtDistrictID.Text == "")
                {
                    DistrictObj.DistrictName = txtDistrictName.Text.ToString();
                    if (DistrictObj.CheckDistrict() == true)
                    {
                        lblError.Text = "Tên chủ đề này đã tồn tại!Vui lòng nhập lại";
                    }
                    else
                    {
                        DistrictObj.activity = "Insert";
                        DistrictObj.DistrictName = txtDistrictName.Text.Trim();
                        DistrictObj.DistrictCode = txtDistrictCode.Text.Trim();
                        DistrictObj.ProvinceID = int.Parse(cboProvinceID.SelectedValue.ToString());
                        DistrictObj.Note = txtNote.Text.Trim();
                        DistrictObj.Creator = Session["AdminName"].ToString();
                        DistrictObj.InsertUpdate();
                        fnLoadData();
                        lblError.Text = "Thêm mới thành công";
                        fnClearControl();
                    }
                }
                else
                {
                    DistrictObj.activity = "Update";
                    DistrictObj.DistrictID = Convert.ToInt32(txtDistrictID.Text.Trim());
                    DistrictObj.DistrictName = txtDistrictName.Text.Trim();
                    DistrictObj.DistrictCode = txtDistrictCode.Text.Trim();
                    DistrictObj.ProvinceID = int.Parse(cboProvinceID.SelectedValue.ToString());
                    DistrictObj.Note = txtNote.Text.Trim();
                    DistrictObj.Creator = Session["AdminName"].ToString();
                    DistrictObj.InsertUpdate();
                    fnLoadData();
                    lblError.Text = "Cập nhập thành công";
                    fnClearControl();
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                string strID = "";
                for (int i = 0; i < dtgDistrict.Items.Count; i++)
                {
                    if (((CheckBox)dtgDistrict.Items[i].FindControl("chkSelect")).Checked == true)
                    {
                        strID += dtgDistrict.Items[i].Cells[0].Text.Trim() + "$";//Cot dau tien phai de cai ID                         
                    }
                }
                CLSCommon.DeleteListRecord("SP_District", strID, "DistrictID");
                fnLoadData();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        public void fnLoadDataByID(int IntID)
        {
            try
            {
                QuanCafe.BusinessLayer.District District = new QuanCafe.BusinessLayer.District();
                District.DistrictID = IntID;
                DataRow dr = District.GetByID();
                txtDistrictID.Text = IntID.ToString();
                txtDistrictName.Text = dr["DistrictName"].ToString();
                txtDistrictCode.Text = dr["DistrictCode"].ToString();
                cboProvinceID.SelectedValue = dr["ProvinceID"].ToString();
                txtNote.Text = dr["Note"].ToString();

            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        protected void dtgDistrict_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            QuanCafe.BusinessLayer.District District = new QuanCafe.BusinessLayer.District();
            string nName = e.CommandName.ToLower();
            int ID = -1;
            switch (nName)
            {
                case "edit":
                    ID = Convert.ToInt32(dtgDistrict.Items[e.Item.ItemIndex].Cells[0].Text);
                    fnLoadDataByID(ID);
                    break;
                case "activechange":
                    ID = int.Parse(dtgDistrict.Items[e.Item.ItemIndex].Cells[0].Text);
                    District.DistrictID = ID;
                    District.ChangeActive();
                    break;
            }
            this.fnLoadData();
        }

        protected void dtgDistrict_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            dtgDistrict.CurrentPageIndex = e.NewPageIndex;
            this.fnLoadData();
        }
    }
}
